package com.tange.module.message.query;


import com.alibaba.fastjson.JSONObject;
import com.tange.core.backend.service.response.RxResponse;
import com.tange.core.backend.service.response.RxResponseTransform;
import com.tg.data.http.entity.AlertMessageFilterBean;
import com.tg.data.http.entity.ChangeNotifyBean;
import com.tg.data.http.entity.MessageDataBean;
import com.tg.data.http.entity.MessageTipDataBean;
import com.tg.data.http.entity.NotifyDataBean;
import com.tg.network.socket.http.TGHttp;

import java.util.HashMap;
import java.util.List;

import io.reactivex.Observable;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;

/**
 * 消息相关查询能力
 */
public class DeviceMessageQuery {

    /**
     * 查询消息列表
     * @param object
     * @return
     */
    public static Observable<RxResponse<MessageDataBean>> queryMessages(JSONObject object) {
        return Observable.create(emitter -> {
            TGHttp.getInstance().getMessageList(object)
                    .subscribeOn(Schedulers.io())
                    .observeOn(AndroidSchedulers.mainThread())
                    .subscribe(new RxResponseTransform(emitter));

        });
    }

    /**
     * 查询消息筛选项
     * @param uuids 设备UUID，多个设备时以逗号","分割
     * @return
     */
    public static Observable<RxResponse<List<AlertMessageFilterBean>>> queryFilters(String uuids) {
        return Observable.create(emitter -> {
            HashMap<String, String> map = new HashMap<>();
            map.put("uuid", uuids);
            TGHttp.getInstance().getMessageFilter(map)
                    .subscribeOn(Schedulers.io())
                    .observeOn(AndroidSchedulers.mainThread())
                    .subscribe(new RxResponseTransform(emitter));

        });
    }

    /**
     * 删除消息
     * @param map
     * @return
     */
    public static Observable<RxResponse<String>> deleteMessages(HashMap<String, String> map) {
        return Observable.create(emitter -> {

            TGHttp.getInstance().deleteMessage(map)
                    .subscribeOn(Schedulers.io())
                    .observeOn(AndroidSchedulers.mainThread())
                    .subscribe(new RxResponseTransform(emitter));

        });
    }

    public static Observable<RxResponse<NotifyDataBean>> readSwitch(String uuid) {
        return Observable.create(emitter -> {
            HashMap<String, String> map = new HashMap<>();
            map.put("uuid", uuid);
            TGHttp.getInstance().getAlarmMsgSwitcher(map)
                    .subscribeOn(Schedulers.io())
                    .observeOn(AndroidSchedulers.mainThread())
                    .subscribe(new RxResponseTransform(emitter));

        });
    }

    public static Observable<RxResponse<MessageTipDataBean>> getAlarmMsgTip(HashMap<String, String> map) {
        return Observable.create(emitter -> {
            TGHttp.getInstance().getAlarmMsgTip(map)
                    .subscribeOn(Schedulers.io())
                    .observeOn(AndroidSchedulers.mainThread())
                    .subscribe(new RxResponseTransform(emitter));

        });
    }


    public static Observable<RxResponse<ChangeNotifyBean>> updateSwitch(JSONObject object) {
        return Observable.create(emitter -> {
            TGHttp.getInstance().changeNotifySetting(object)
                    .subscribeOn(Schedulers.io())
                    .observeOn(AndroidSchedulers.mainThread())
                    .subscribe(new RxResponseTransform(emitter));

        });
    }

}
